package util;
import processing.core.PApplet;
import processing.core.PFont;

public class Graph {
	PFont font;
	private float maxVal, minVal;
	private float x, y, height, width;
	private float[] val;
	private float[] lines; 
	private float[] markers;
	float r, g, b;
	int xDiv;
	int yDiv;
	
	public Graph(int length, int xDiv, float maxVal, float minVal, int yDiv, float[] lines, float x, float y, float width, float height, PApplet pa) {
		this.maxVal = maxVal;
		this.minVal = minVal;
		this.lines = lines;
		initLines();
		this.xDiv = xDiv;
		this.yDiv = yDiv;
		this.x = x;
		this.y = y;
		this.height = height;
		this.width = width;
		val = new float[length];
		markers = new float[length];
		r = 255;
		g = 255;
		b = 255;
		font = pa.loadFont("LiGothicMed-48.vlw");
		
	}
	
	public void addVal(float v){
		for(int i = 0; i < val.length - 1; i++){
			val[i] = val[i+1];
			markers[i] = markers[i+1];
		}
		val[val.length-1] = (v + minVal) * height / (maxVal - minVal);
	}
	
	public void addMarker(int m){
		markers[markers.length-1] = m;
	}
	
	private void initLines(){
		for(int i = 0; i < lines.length; i++)
			lines[i] = (lines[i] + minVal) * height / (maxVal - minVal);
	}
	
	public void setLines(float[] lines){
		this.lines = lines;
		initLines();
	}
	
	public void setRange(float min, float max){
		for(int i = 0; i < val.length; i++){
			val[i] = (val[i] - minVal) * (maxVal - minVal) / height;
			val[i] = (val[i] + min) * height / (max - min);
		}
		minVal = min;
		maxVal = max;
	}
	
	
	public void setColor(float r, float g, float b){
		this.r = r;
		this.g = g;
		this.b = b;
	}
	
	public void draw(PApplet pa){
		pa.noFill();
		pa.strokeWeight(1);
		pa.stroke(r * 0.66f, g * 0.66f, b * 0.66f);
		
		for(int i = 0; i < lines.length; i++)
			pa.line(x, lines[i], x + width, lines[i]);
		pa.line(x, y, x + width, y);
		pa.line(x, y + height, x + width, y + height);
		
		pa.stroke(r * 0.33f, g * 0.33f, b * 0.33f);
		float yStep = height / yDiv;
		for(int i = 0; i < yDiv; i++){
			float cy = y + yStep * i;
			pa.line(x, cy, x + width, cy);
		}
		
		float xStep = width / xDiv;
		for(int i = 0; i < xDiv; i++){
			float cx = x + xStep * i;
			float cy = height / 20;
			pa.line(cx, y, cx, y + cy);
			pa.line(cx, y + height, cx, y + height - cy);
		}
		
		pa.textFont(font, 12);
		pa.noStroke();
		pa.fill(r, g, b);
		
		pa.text("" + maxVal, x + width - 20, y + height - 10);
		pa.text("" + minVal, x + width - 20, y + 10);
		pa.textFont(font, 9);
		pa.text("yDiv:" + (maxVal - minVal) / yDiv, x + width - 20, y + 18);
		pa.text("xDiv:" + val.length / xDiv, x + width - 20, y + height - 18);
		
		float stepSize = width / val.length;
		
		pa.noFill();
		pa.stroke(240, 20, 20);
		
		for(int i = 0; i < markers.length; i++){
			if(markers[i] == 1){
				pa.stroke(240, 20, 20);
				pa.line(x + i*stepSize, y + height/2f, x + i*stepSize, y + height);
			}
			if(markers[i] == 2){
				pa.stroke(240, 20, 20);
				pa.line(x + i*stepSize, y + height/2f, x + i*stepSize, y);
			}
			if(markers[i] == 3){
				pa.stroke(20, 20, 240);
				pa.line(x + i*stepSize, y + height, x + i*stepSize, y);
			}
		}
		
		pa.strokeWeight(1);
		pa.stroke(r, g, b);

		for(int i = 0; i < val.length - 1; i++)
			pa.line(x + i*stepSize, y - val[i], x + (i+1) * stepSize, y - val[i+1]);
		
	}
}
